﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mNC.Calculate
{
    public class AL_CPCK
    {
        /// <summary>
        /// CPCK_HEAD
        /// </summary>
        /// <param name="TOOL_NO">刀具号</param>
        /// <param name="TOOL_DIA">刀具直径</param>
        /// <param name="NOSECODE">机头代码</param>
        /// <param name="ROTATE_SPEED">转速</param>  
        /// <returns></returns>
        public static string GET_AL_CPCK_HEAD(string TOOL_NO, string TOOL_DIA, string NOSECODE, string ROTATE_SPEED)
        {
            StringBuilder str = new StringBuilder("(CIRCLE_POCKET_HEAD)");
            str.Append("\r\n");
            str.Append("G00G91G28Z0.");
            str.Append("\r\n");
            str.Append("G00G91G28Y0.");
            str.Append("\r\n");
            str.Append("(TOOL_" + TOOL_NO + "_DIA_" + TOOL_DIA + ")");
            str.Append("\r\n");
            str.Append("M06" + TOOL_NO + "");
            str.Append("\r\n");
            str.Append("G00G90" + NOSECODE + "M03S" + ROTATE_SPEED);
            str.Append("\r\n");
            str.Append("(CIRCLE_POCKET_HEAD)");
            str.Append("\r\n");
            str.Append("");
            str.Append("\r\n");
            return str.ToString();
        }

        /// <summary>
        /// CPCK_CYCLE
        /// </summary>
        /// <param name="TOOL_NO">刀具号</param>
        /// <param name="FACE_HEIGHT">安全高度</param>
        /// <param name="INITIAL_HEIGHT">初始高度</param>
        /// <param name="PROCESS_DEPTH">加工深度</param>
        /// <param name="FEED_SPEED_V">垂直进给</param>
        /// <param name="COORD_X">X坐标</param>
        /// <param name="COORD_Y">Y坐标</param>
        /// <param name="FEATURE_RADIUS">特征半径</param>
        /// <param name="CIR_GROOVE_LINE">圆槽直线</param>
        /// <returns></returns>
        public static string GET_AL_CPCK_CYCLE(DataTable dt_TEMP)
        //(string TOOL_NO, string FACE_HEIGHT, string INITIAL_HEIGHT,
        //string PROCESS_DEPTH, string FEED_SPEED_V, string COORD_X, string COORD_Y, double FEATURE_RADIUS, double CIR_GROOVE_LINE)
        {
            StringBuilder str = new StringBuilder("(CIRCLE_POCKET_CYCLE)");
            str.Append("\r\n");
            for (int i = 0; i < dt_TEMP.Rows.Count; i++)
            {
                string COMPENSATION_NO = dt_TEMP.Rows[i]["COMPENSATION_NO"].ToString();
                string FACE_HEIGHT = Math.Round(Convert.ToDouble(dt_TEMP.Rows[i]["FACE_HEIGHT"].ToString()) + 60, 1).ToString("0.00");
                string INITIAL_HEIGHT = dt_TEMP.Rows[i]["INITIAL_HEIGHT"].ToString().Length > 0 ? Convert.ToDouble(dt_TEMP.Rows[i]["INITIAL_HEIGHT"].ToString()).ToString("0.00") : "";
                string PROCESS_DEPTH = dt_TEMP.Rows[i]["PROCESS_DEPTH"].ToString().Length > 0 ? Convert.ToDouble(dt_TEMP.Rows[i]["PROCESS_DEPTH"].ToString()).ToString("0.00") : "";
                string FEED_SPEED_V = dt_TEMP.Rows[i]["FEED_SPEED_V"].ToString().Length > 0 ? Convert.ToDouble(dt_TEMP.Rows[i]["FEED_SPEED_V"].ToString()).ToString("0.00") : "";
                string COORD_X = Convert.ToDouble(dt_TEMP.Rows[i]["COORD_X"]).ToString("0.00");//因前面设置了类型，故需要转化
                string COORD_Y = dt_TEMP.Rows[i]["COORD_Y"].ToString();
                Double FEATURE_DIA = dt_TEMP.Rows[i]["FEATURE_DIA"].ToString().Length > 0 ? Convert.ToDouble(dt_TEMP.Rows[i]["FEATURE_DIA"].ToString()) : 0;
                string FEATURE_RADIUS = (FEATURE_DIA / 2 - (Convert.ToDouble(dt_TEMP.Rows[i]["TOOL_DIA"].ToString()) / 2)).ToString("0.00");
                string CIR_GROOVE_LINE = (Convert.ToDouble(dt_TEMP.Rows[i]["COORD_X"].ToString()) +
                    (Convert.ToDouble(dt_TEMP.Rows[i]["FEATURE_DIA"].ToString()) / 2) -
                     (Convert.ToDouble(dt_TEMP.Rows[i]["TOOL_DIA"].ToString()) / 2)).ToString("0.00");

                str.Append("X" + COORD_X + "Y-" + COORD_Y + "");
                str.Append("\r\n");
                str.Append("G00G43" + COMPENSATION_NO + "Z" + FACE_HEIGHT + "M08");
                str.Append("\r\n");
                str.Append("G00Z" + INITIAL_HEIGHT + "");
                str.Append("\r\n");
                str.Append("G01Z" + PROCESS_DEPTH + "F" + FEED_SPEED_V + "");
                str.Append("\r\n");
                str.Append("G01X" + CIR_GROOVE_LINE + "Z" + PROCESS_DEPTH + "F800.");
                str.Append("\r\n");
                str.Append("G03I-" + FEATURE_RADIUS + "F800.");
                str.Append("\r\n");
                str.Append("G00Z" + FACE_HEIGHT + "");
                str.Append("\r\n");
            }
            str.Append("(CIRCLE_POCKET_CYCLE)");
            str.Append("\r\n");
            str.Append("");
            str.Append("\r\n");
            return str.ToString();
        }
    }
}
